<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Wy;

use think\Db;

//扩展属性
class Attr {
    public function __construct(){
    }
	public function attr_type(){
		$_lists = Db::name('attr_type')->where('status','=','1')->field('name,type,info')->select();
		return $_lists;
	}
	
	public function get_field_value($_type,$_value){
		// 
		if(strpos($_type,'config_') === 0){
			$_function_name = 'get_'.$_type.'_name';
			return $this->$_function_name($_value);
			// return $_type.'-'.$_value;
		}else{
			$_c_name = 'wy_attr_lists_'.$_type;
			$_lists = cache($_c_name);
			if(!$_lists){
				$_lists = Db::name('attr_lists')
					->where('type','like',$_type)
					->where('status','=','1')
					->field('id,name')
					->order('sort asc,id asc')
					->select();
				cache($_c_name,$_lists,3600);
			}
			$_show_value = '';
			if($_lists){
				foreach($_lists as $k=>$v){
					
					if($v['id'] == $_value){
						$_show_value = $v['name'];
						break;
					}
					
				}
			}
			// return json_encode($_lists);
			return $_show_value;
		}
	}
	
	public function attr_lists($_type,$_value=''){
		if(strpos($_type,'config_') === 0){
			return $this->$_type($_value);
		}else{
			$_c_name = 'wy_attr_lists_'.$_type;
			$_lists = cache($_c_name);
			if(!$_lists){
				$_lists = Db::name('attr_lists')
					->where('type','like',$_type)
					->where('status','=','1')
					->field('id,name')
					->order('sort asc,id asc')
					->select();
				cache($_c_name,$_lists,3600);
			}
			if($_lists){
				foreach($_lists as $k=>$v){
					$_selected = 0;
					if($v['id'] == $_value){
						$_selected = 1;
					}
					$_lists[$k]['selected'] = $_selected;
				}
			}
		}
		return $_lists;
	}
	public function config_area($_value){
	
		$_value = explode(',',$_value);
		$_value = array_filter($_value);
		$_value = array_values($_value);
		
		$_data = array();
		
		$_data[] = $this->_area_lists(0);
		
		if($_value){
			foreach($_value as $k=>$v){
				$_data[] = $this->_area_lists($v);
			}
		}
		if($_data){
			foreach($_data as $k=>$v){
				if($v){
					foreach($v as $aa=>$bb){
						$_selected = 0;
						$_value_k = isset($_value[$k])?$_value[$k]:0;
						if($bb['id'] == $_value_k){
							$_selected = 1;
						}
						$_data[$k][$aa]['selected'] = $_selected;
					}
				}else{
					unset($_data[$k]);
				}
			}
		}
		return $_data;
		// return 'config_area';
	}
	public function get_config_area_name($_value){
		$_value = explode(',',$_value);
		$_value = array_filter($_value);
		$_value = array_values($_value);
		if(!$_value){
			return '';
		}
		$_lists_all = $this->get_all_area_lists();
		$_lists = '';
		for($i=0;$i<count($_value);$i++){
			foreach($_lists_all as $k=>$v){
				if($v['id'] == $_value[$i]){
					if($_lists == ''){
						$_lists .= $v['name'];
					}else{
						$_lists .= ' - '.$v['name'];
					}
					// break;
				}
			}
		}
		return $_lists;
	}
	public function _area_lists($up_id=0){
		$_lists_all = $this->get_all_area_lists();
		$_lists = array();
		if($_lists_all){
			foreach($_lists_all as $k=>$v){
				if($v['up_id'] == $up_id){
					$_lists[] = $v;
				}
			}
		}
		return $_lists;
	}
	public function get_all_area_lists(){
		$_c_name = 'wy_get_all_area_lists';
		$_lists = cache($_c_name);
		if($_lists){
			return $_lists;
		}
		$_lists = Db::name('area')
			->field('id,name,up_id')
			->order('id asc')
			->select();
		cache($_c_name,$_lists,3600);	
		return $_lists;
	}
}
